package com.example.applyforaid.controller;

import com.example.applyforaid.entity.ManagerUser;
import com.example.applyforaid.entity.StudentUser;
import com.example.applyforaid.service.impl.ManagerUserServiceImpl;
import com.example.applyforaid.service.impl.StudentUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class UserLoginController {
    @Autowired
    private StudentUserServiceImpl studentUserService;

    @Autowired
    private ManagerUserServiceImpl managerUserService;

    @RequestMapping(value = "Login/",method = RequestMethod.POST)
    public ModelMap studentUserLogin(String uid, String pwd, HttpServletRequest request){
        ModelMap modelMap = new ModelMap();
        if(uid.length() == 20) {
            ManagerUser mu = managerUserService.getUserAndVerifyService(uid,pwd);
            if(mu == null)
                return modelMap.addAttribute("userType",-1);
            request.getSession().setAttribute("user", mu);
            modelMap.addAttribute("user",mu);
            modelMap.addAttribute("userType",mu.getManagerRole());
            return modelMap;
        }
        else {
            StudentUser studentUser = studentUserService.getUserAndVerifyService(uid,pwd);
            if(studentUser == null)
                return modelMap.addAttribute("userType",-1);
            request.getSession().setAttribute("user", studentUser);
            modelMap.addAttribute("user",studentUser);
            modelMap.addAttribute("userType",0);
            return modelMap;
        }
    }

}
